package com.soft.idc.module.kpi;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class KpiService {

	public static Map<String, List<Record>> loadKpiRecords(List<KpiVo> kvoList, Date startTime, Date endTime) {
		String sql = "select value,kpi_id,obj_id,create_time from idc_kpi_record where kpi_id = ? and obj_id = ? "
				+ "and create_time between ? and ? order by create_time asc";
		Map<String, List<Record>> map = new HashMap<String, List<Record>>();
		for (KpiVo vo : kvoList) {
			List<Record> result = Db.find(sql, vo.getKpiId(), vo.getObjId(), startTime, endTime);
			map.put(vo.getKpiId() + "", result);
		}
		return map;
	}

	public static Map loadKpiRecords(List<KpiVo> kvoList, Date startTime, Date endTime, long stepTime)
			throws ParseException {
		String sql = "select value,kpi_id,obj_id,create_time from idc_kpi_record where kpi_id = ? and obj_id = ? "
				+ "and create_time between ? and ? order by create_time desc";
		Map<String, Map<Date, Record>> recordMap = new HashMap<String, Map<Date, Record>>();
		Date maxTime = null;
		Date minTime = null;
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:00");
		for (KpiVo vo : kvoList) {
			List<Record> result = Db.find(sql, vo.getKpiId(), vo.getObjId(), startTime, endTime);
			Map<Date, Record> drMap = new HashMap<Date, Record>();
			for (Record rvo : result) {
				Date d = rvo.getTimestamp("create_time");
				String str = dateFormat.format(d);
				d = dateFormat.parse(str);
				drMap.put(d, rvo);
			}
			recordMap.put(vo.getKpiId() + "", drMap);

			Date curMaxTime = result.get(0).getTimestamp("create_time");
			Date curMinTime = result.get(result.size() - 1).getTimestamp("create_time");
			if (maxTime == null) {
				maxTime = curMaxTime;
			} else {
				if (maxTime.before(curMaxTime)) {
					maxTime = curMaxTime;
				}
			}
			if (minTime == null) {
				minTime = curMinTime;
			} else {
				if (minTime.after(curMinTime)) {
					minTime = curMinTime;
				}
			}
		}

		String minTimeStr = dateFormat.format(minTime);
		String maxTimeStr = dateFormat.format(maxTime);
		minTime = dateFormat.parse(minTimeStr);
		maxTime = dateFormat.parse(maxTimeStr);

		List<Date> xAxisList = new ArrayList<Date>();
		long maxTime0 = maxTime.getTime();
		xAxisList.add(maxTime);
		while (maxTime0 >= minTime.getTime()) {
			maxTime0 -= stepTime * 60 * 1000L;
			xAxisList.add(new Date(maxTime0));
		}

		Map<String, Object> result = new HashMap<String, Object>();
		Set<String> keys = recordMap.keySet();
		Iterator it = keys.iterator();
		while (it.hasNext()) {
			String key = (String) it.next();
			Map<Date, Record> kpiMap = recordMap.get(key);
			List<String> newList = new ArrayList<String>();
			for (Date d : xAxisList) {
				String value = "0";
				if (kpiMap.containsKey(d)) {
					value = kpiMap.get(d).get("value");
				}
				newList.add(value);
			}
			result.put(key, newList);
		}
		return result;
	}

}
